package com.ticket.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import com.ticket.common.constant.PerformConstants;
import com.ticket.common.core.domain.entity.SysDictData;
import com.ticket.common.utils.DateUtils;
import com.ticket.system.domain.YanchuScore;
import com.ticket.system.domain.YanchuScoreDetail;
import com.ticket.system.mapper.YanchuScoreMapper;
import com.ticket.system.service.ISysDictDataService;
import com.ticket.system.service.IYanchuScoreDetailService;
import com.ticket.system.service.IYanchuScoreService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ticket.system.mapper.TYanchuPointsRuleMapper;
import com.ticket.system.domain.TYanchuPointsRule;
import com.ticket.system.service.ITYanchuPointsRuleService;

/**
 * 积分规则Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-18
 */
@Service
public class TYanchuPointsRuleServiceImpl implements ITYanchuPointsRuleService 
{
    @Autowired
    private TYanchuPointsRuleMapper tYanchuPointsRuleMapper;
    @Autowired
    private YanchuScoreMapper yanchuScoreMapper;
    @Autowired
    private IYanchuScoreDetailService iYanchuScoreDetailService;
    @Autowired
    private IYanchuScoreService iYanchuScoreService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    private static final Logger logger = LoggerFactory.getLogger(TYanchuPointsRuleServiceImpl.class);

    /**
     * 查询积分规则
     * 
     * @param id 积分规则主键
     * @return 积分规则
     */
    @Override
    public TYanchuPointsRule selectTYanchuPointsRuleById(Long id)
    {
        return tYanchuPointsRuleMapper.selectTYanchuPointsRuleById(id);
    }

    /**
     * 查询积分规则列表
     * 
     * @param tYanchuPointsRule 积分规则
     * @return 积分规则
     */
    @Override
    public List<TYanchuPointsRule> selectTYanchuPointsRuleList(TYanchuPointsRule tYanchuPointsRule)
    {
        return tYanchuPointsRuleMapper.selectTYanchuPointsRuleList(tYanchuPointsRule);
    }

    /**
     * 新增积分规则
     * 
     * @param tYanchuPointsRule 积分规则
     * @return 结果
     */
    @Override
    public int insertTYanchuPointsRule(TYanchuPointsRule tYanchuPointsRule)
    {
        tYanchuPointsRule.setCreateTime(DateUtils.getNowDate());
        return tYanchuPointsRuleMapper.insertTYanchuPointsRule(tYanchuPointsRule);
    }

    /**
     * 修改积分规则
     * 
     * @param tYanchuPointsRule 积分规则
     * @return 结果
     */
    @Override
    public int updateTYanchuPointsRule(TYanchuPointsRule tYanchuPointsRule)
    {
        tYanchuPointsRule.setUpdateTime(DateUtils.getNowDate());
        return tYanchuPointsRuleMapper.updateTYanchuPointsRule(tYanchuPointsRule);
    }

    /**
     * 批量删除积分规则
     * 
     * @param ids 需要删除的积分规则主键
     * @return 结果
     */
    @Override
    public int deleteTYanchuPointsRuleByIds(Long[] ids)
    {
        return tYanchuPointsRuleMapper.deleteTYanchuPointsRuleByIds(ids);
    }

    /**
     * 删除积分规则信息
     * 
     * @param id 积分规则主键
     * @return 结果
     */
    @Override
    public int deleteTYanchuPointsRuleById(Long id)
    {
        return tYanchuPointsRuleMapper.deleteTYanchuPointsRuleById(id);
    }


    @Override
    public void addPoints(int ruleType, BigDecimal money,Long userId) {
        logger.info("addPoints ruleType:{} money:{} userId:{}",ruleType,money,userId);
        TYanchuPointsRule tYanchuPointsRule = new TYanchuPointsRule();
        tYanchuPointsRule.setRuleType(ruleType);
        List<TYanchuPointsRule> tYanchuPointsRules = tYanchuPointsRuleMapper.selectTYanchuPointsRuleList(tYanchuPointsRule);
        if(CollectionUtils.isNotEmpty(tYanchuPointsRules)){
            if(tYanchuPointsRules.get(0).getStatus()==2){
                return;
            }
            Integer pointsType = tYanchuPointsRules.get(0).getPointsType();
            BigDecimal pointsNum = tYanchuPointsRules.get(0).getPointsNum();
            if(pointsType==2&&money!=null){
                BigDecimal pointsUnint = tYanchuPointsRules.get(0).getPointsUnint();
                if(money.compareTo(pointsUnint) < 0){
                    return;
                }
                //取整
                pointsNum = money.divide(pointsUnint,0,RoundingMode.DOWN).multiply(pointsNum);
            }
            //添加积分明细
            YanchuScoreDetail yanchuScoreDetail = new YanchuScoreDetail();
            yanchuScoreDetail.setCreateTime(new Date());
            yanchuScoreDetail.setUpdateTime(new Date());
            yanchuScoreDetail.setPointsNum(pointsNum);
            yanchuScoreDetail.setPointsRuleId(tYanchuPointsRules.get(0).getId());
            yanchuScoreDetail.setUserId(userId);
            iYanchuScoreDetailService.insertYanchuScoreDetail(yanchuScoreDetail);
            //更新到积分表
            int scoreCount = yanchuScoreMapper.updateScore(userId,pointsNum.intValue());
            if(scoreCount == 0){
                YanchuScore yanchuScore = new YanchuScore();
                yanchuScore.setWxUserId(userId);
                yanchuScore.setScore(pointsNum.intValue());
                yanchuScore.setStatus(PerformConstants.SCORE_STATUS_INIT);
                yanchuScore.setCreateTime(new Date());
                yanchuScore.setUpdateTime(new Date());
                yanchuScoreMapper.insertYanchuScore(yanchuScore);
            }
        }
    }

    public void resetScore(){
        //判断是哪种类型
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("yanchu_points_type");
        List<SysDictData> selectDictDataList = sysDictDataService.selectDictDataList(sysDictData);
        if(CollectionUtils.isNotEmpty(selectDictDataList)){
            String dictValue = selectDictDataList.get(0).getDictValue();
            logger.info("==================="+dictValue+"====================");
            if("2".equals(dictValue)){
                logger.info("==================="+dictValue+"====================");
                iYanchuScoreService.resetScore();
            }
        }
    }
}
